package com.infinitegroup.findmystuff.activities;

import java.util.List;
import java.util.Map;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.HomePresenter;
import com.infinitegroup.findmystuff.views.HomeView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity implements HomeView{
	
	private HomePresenter presenter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) { // long method. feel free to decompose.
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userhome); // update later with items.
		
		Bundle extras = getIntent().getExtras();
		String value = extras.getString("username"); // retrieve the username passed in by login.
		presenter = new HomePresenter(this, value);		
		
		
		// Update the welcome message
		((TextView) findViewById(R.id.userhome_welcome)).setText("Welcome " + presenter.getFirstName());
		
		/*
		// Get the items to display.
		List<Map<String, String>> itemList = presenter.getItemsToDisplay();
		String itemString = generateItemString(itemList);
		((TextView) findViewById(R.id.userhome_itemfilltext)).setText(itemString); // temporary. must make these radio buttons.
		*/
		
		// Add button listeners.
		Button addItem = (Button) findViewById(R.id.userhome_add_activity);
		Button deleteItem = (Button) findViewById(R.id.userhome_delete_activity);
		Button seeAllItems = (Button) findViewById(R.id.userhome_see_all_items);
		Button logout = (Button) findViewById(R.id.userhome_logout);
		addItem.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				presenter.handleAddItemClick();
			}
		});
		deleteItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				presenter.handleDeleteItemsClick();
			}
		});
		seeAllItems.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				presenter.handleSeeAllItemsClick();
				
			}
		});
		logout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				presenter.handleLogoutClick();
				
			}
		});
		
	}
	
	public void onResume() {
	    super.onResume();  // Always call the superclass method first
	    // Get the items to display.
	 	List<Map<String, String>> itemList = presenter.getItemsToDisplay();
	 	String itemString = generateItemString(itemList);
	 	((TextView) findViewById(R.id.userhome_itemfilltext)).setText(itemString);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK)){
	        finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * Temporary method.
	 * Generates a formatted string for all items.
	 */
	public String generateItemString(List<Map<String, String>> accountItems) {
		String result = "";
		if (accountItems!=null){
			for (Map<String,String> m : accountItems){
				result = result + "Name: " + m.get("itemname") + " | " + m.get("status") + "\n" + "Category: " +m.get("category") + "\n";
			}
		}
		return result;
	}
	
	@Override
	public void advanceToAddItemView(String username) {
		Intent intent = new Intent(this, AddItemActivity.class);
		intent.putExtra("username", username);
		startActivity(intent);
		
	}

	@Override
	public void displayMessage(String message) {
		Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	@Override
	public void updateWelcome(String firstName) {
		//Quite redundant.
	}

	@Override
	public void generateHomeScreen(List<Map<String, String>> items) {
		
	}

	@Override
	public void advanceToViewAllView() {
		Intent intent = new Intent(this, ViewAllItemsActivity.class);
		startActivity(intent);
	}

	@Override
	public void advanceToLogin() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}

	@Override
	public void end() {
		this.finish();
	}

}
